const app = getApp();
import {
    getQuestionSurveyList,
    deleteQuestion
    // getquestion
} from "../../api/questionnaire"

Page({

    /**
     * 页面的初始数据
     */
    data: {
        quesList: [], //问卷列表
        totalPage: 1,
        showCount: 10,
        currentPage: 1,
    },
    //#region  生命周期

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) { },
    onShow: function (options) {
       this.initQuestionList();
    },
    onShareAppMessage: function () {

    },
    onReachBottom: function () {
        let { currentPage, totalPage } = this.data;
        if (currentPage < totalPage) {
            currentPage += 1;
            this.setData({
                currentPage
            });
            setTimeout(() => {
                this.fetchQuestionList();
            }, 300);
        } else {
            wx.showToast({
                title: '没有更多数据了!',
                icon: 'none'
            });
        }
    },
    // 初始化问卷列表
    initQuestionList() {
        this.setData({
            quesList: [],
            currentPage: 1,
            totalPage: 1
        });
        this.fetchQuestionList();
    },
    // 获取问卷列表
    fetchQuestionList() {
        getQuestionSurveyList({
            "currentPage": this.data.currentPage,
            "showCount": this.data.showCount,
            "type": "1"
        }).then((res) => {
            console.log(res);
            this.setData({
                totalPage: res.data.totalPage,
                quesList: this.data.quesList.concat(res.data.list)
            })
        })
    },
    /**
     * 拨打电话
     * @param {*} e 
     */
    freeTell(e) {
        const {
            phone
        } = e.target.dataset
        if (phone == 0) {
            wx.showToast({
                title: '未填写电话',
                icon: "none"
            })
        } else {
            wx.makePhoneCall({
                phoneNumber: e.target.dataset.phone,
            });
        }
    },
    /**
     * 前往详情页
     * @param {*} e 
     */
    goToDetail(e) {

        let {
            splid
        } = e.currentTarget.dataset;
        wx.navigateTo({
            url: `/pages/questionaire/out/out?splid=${splid}`,
        })
    },
    /**
     * 分享问卷
     * @param {*} e 
     */
    goToShare(e) {
        const {
            questitle,
            splid
        } = e.currentTarget.dataset;;
        wx.navigateTo({
            url: `/pages/questionaire/sharequestionnaire/sharequestionnaire?&splid=${splid}&questitle=${questitle}`,
        })
    },
    /**
     * 新建问卷
     * @param {*} e 
     */
    toCreatePage(e) {
        wx.navigateTo({
            url: '/pages/questionaire/createquestionnaire/createQues',
        })
    },
    /**
     *  删除问卷 确认
     */
    promptDeleteQuestion(e) {
        const {
            question_id,
            splid,
            title
        } = e.currentTarget.dataset
        wx.showModal({
            content: `确认删除 ${title} 吗`,
            success: (e) => {

                if (e.confirm) {
                    this.fetchDeleteQuestion(question_id, splid)
                }
                else if (e.cancel) {
                    wx.showToast({
                        title: '取消删除',
                        icon: 'none'
                    })
                }
            }
        })

    },
    /**
     * 删除问卷
     * @param {*} question_id 
     * @param {*} splid 
     */
    fetchDeleteQuestion(question_id, splid) {
        deleteQuestion({
            splid,
            question_id
        }).then(res => {
            if (res) {
                wx.showToast({
                    title: '删除成功',
                    icon: 'none'
                })
                setTimeout(() => {
                  this.initQuestionList();
                }, 800)
            }
        })
            .catch(err => {

            })
    }
    //#endregion 
})